﻿#region Using directives
using Microsoft.VisualStudio.TestTools.UnitTesting;

using System.Collections.Generic;

using Weazel.Gezel.Model;
using Weazel.Gezel.Model.Errors;
using Weazel.Math;
#endregion

namespace Weazel.Gezel.Parser.Test.Expressions
{
  [TestClass]
  public class TestParseExpression
  {
    [TestMethod]
    public void TestParseExpression1()
    {
      Tokenizer t = new Tokenizer("a = b + 2");

      Expression e = ParseExpression.Parse(new Set<Token>(Token.Eof), t);

      Assert.IsTrue(e is BinaryExpression);

      BinaryExpression be1 = e as BinaryExpression;

      Assert.AreEqual(ExpressionType.Assignment, be1.ExpressionType);
      
      // check left child of be1
      Assert.IsTrue(be1.LeftChild is LeafExpression);
      Assert.AreEqual("a", ((LeafExpression)be1.LeftChild).Value);

      // check right child of be1
      Assert.IsTrue(be1.RightChild is BinaryExpression);
      BinaryExpression be2 = be1.RightChild as BinaryExpression;

      // check left child of be2
      Assert.IsTrue(be2.LeftChild is LeafExpression);
      Assert.AreEqual(ExpressionType.Identifier, ((LeafExpression)be2.LeftChild).ExpressionType);
      Assert.AreEqual("b", ((LeafExpression)be2.LeftChild).Value);

      // check right child of be2
      Assert.IsTrue(be2.RightChild is LeafExpression);
      Assert.AreEqual(ExpressionType.Constant, ((LeafExpression)be2.RightChild).ExpressionType);
      Assert.AreEqual("2", ((LeafExpression)be2.RightChild).Value);
    }
  }
}
